$(function () {
    $("#jqGrid").Grid({
        url: '../teamrecommend/list',
        colModel: [
			{label: 'uid', name: 'uid', index: 'uid', key: true, hidden: true},
			{label: '用户昵称', name: 'userName', index: 'user_name', width: 80,align:'center'},
			{label: '头像', name: 'logo', index: 'logo', width: 80,align:'center',
				formatter:function (value) {
					return transImg(value);
				}
			},
			{label: '手机号', name: 'phone', index: 'phone', width: 80,align:'center'},
			{label: '团队编号', name: 'tid', index: 'tid', width: 80,align:'center'},
			{label: '排序', name: 'sortOrder', index: 'sort_order', width: 80,align:'center'},
			{label: '创建时间', name: 'createDate', index: 'create_date', width: 80,align:'center',
				formatter:function (value) {
					return transDate(value, 'yyyy-MM-dd hh:mm:ss');
				}
			}]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		teamRecommend: {
        	uid:'',
			userName:'',
			logo:'',
			phone:'',
			tid:'',
			sortOrder:''
		},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		},
		userphone:''
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.teamRecommend = {
				uid:'',
				userName:'',
				logo:'',
				phone:'',
				tid:'',
				sortOrder:''
			};
		},
		update: function (event) {
            let uid = getSelectedRow("#jqGrid");
			if (uid == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(uid)
		},
		saveOrUpdate: function (event) {
            let url = "../teamrecommend/save" ;
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.teamRecommend),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let uids = getSelectedRows("#jqGrid");
			if (uids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../teamrecommend/delete",
                    params: JSON.stringify(uids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(uid){
            Ajax.request({
                url: "../teamrecommend/info/"+uid,
                async: true,
                successCallback: function (r) {
                    vm.teamRecommend = r.teamRecommend;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },searchuser:function () {
			Ajax.request({
				url: "../teamrecommend/queryObjectByPhone?phone="+vm.userphone,
				async: true,
				successCallback: function (r) {
					vm.teamRecommend.uid = r.model.uid;
					vm.teamRecommend.userName = r.model.uname;
					vm.teamRecommend.logo = r.model.upicurl;
					vm.teamRecommend.phone = r.model.umobile;
					vm.teamRecommend.tid = r.model.teamid;
				}
			});
		}
	}
});